package com.ithei.多线程第二天.送快递3;

public class Demo {
    public static void main(String[] args) {
        new Thread(() -> {
            while (true) {
                synchronized (Demo.class) {
                    if (Kd.count == 0) {
                        break;
                    } else {
                        if (Kd.flag) {
                            try {
                                Demo.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Kd.count--;
                            Kd.flag = true;
                            System.out.println(Thread.currentThread().getName() + "在执行");
                            Demo.class.notifyAll();
                        }
                    }
                }
            }
        }, "快递员").start();

        new Thread(() -> {
            while (true) {
                synchronized (Demo.class) {
                    if (Kd.count == 0) {
                        break;
                    } else {
                        if (!Kd.flag) {
                            try {
                                Demo.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Kd.flag = false;
                            System.out.println(Thread.currentThread().getName() + "在执行");
                            Demo.class.notifyAll();
                        }
                    }
                }
            }
        }, "用户").start();
    }
}
